//
//  PSTabBar.m
//  UITabBar的不规则显示
//
//  Created by ibos on 15/12/28.
//  Copyright © 2015年 ibos. All rights reserved.
//

#import "PSTabBar.h"
#import "UIView+Ext.h"
#define IsKindOfClass(x, y) [x isKindOfClass:[y class]]
@interface PSTabBar ()
{
    NSMutableArray * _tabBarItems;
    
}
@property (nonatomic,weak)UIView * layerLine;
@property (nonatomic,weak)UIButton * irregularBtn;
@end
@implementation PSTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _tabBarItems = [@[] mutableCopy];
        
        [self setupLayerLine];
        
        [self setupIrregularButton];
        
    }
    return self;
}
- (void)setupLayerLine
{
    // 添加边界线
    UIView * lineView = [UIView new];
    lineView.backgroundColor = [UIColor clearColor];
    [self addSubview:lineView];
    self.layerLine = lineView;
}

- (void)setupIrregularButton
{
    // 添加中间不规则按钮
    UIButton * irBtn = [UIButton new];
    
    irBtn.backgroundColor = [UIColor whiteColor];

    [irBtn setImage:[UIImage imageNamed:@"Tab_mai"] forState:UIControlStateNormal];
    
    [irBtn setImage:[UIImage imageNamed:@"Tab_mai"] forState:UIControlStateSelected];
    
    irBtn.adjustsImageWhenHighlighted = NO;
    
    [irBtn addTarget:self action:@selector(clickEvent:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self addSubview:irBtn];
    self.irregularBtn = irBtn;
    
}
- (void)clickEvent:(UIButton*)sender
{
    if (_block)_block();
    
}

#pragma mark -布局子控件-
- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"---子控件 %@----",self.subviews);
    [self adjustNewSubViewFrame];
    [self removeLayerLine];
    [self getAllTabBarButtons];
//    [self adjustAllTabBarButtonsFrame];
}

- (void)adjustNewSubViewFrame
{
    // 调整新加入控件的frame
    self.irregularBtn.size = CGSizeMake(60, 60); // 大小自己调整
    self.irregularBtn.centerX = self.width * 0.5;
    self.irregularBtn.y = self.height - self.irregularBtn.height - 5; //高度具体调整
    setBoundsWithView(self.irregularBtn, self.irregularBtn.width / 2 );
    setBorderWithColor(self.irregularBtn, 0.5,self.layerLine.backgroundColor);
    
    self.layerLine.x = 0;
    self.layerLine.y = 0.5;
    self.layerLine.width = self.width;
    self.layerLine.height = 0.5;

}
- (void)removeLayerLine
{
    // 简单粗暴先干掉系统的边界线
    for (UIView * view in self.subviews)
    {
        if (IsKindOfClass(view, UIImageView))
        {
            [view removeFromSuperview];
            break; // 必须跳出循环，否则循环过程中删除 会导致数量错失
        }
    }
}

- (void)getAllTabBarButtons
{
    for (UIView *tabBarButton in self.subviews)
    {
        // 如果不是UITabBarButton， 直接跳过
        if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        [_tabBarItems  addObject:tabBarButton];
    }
    UIView * view = (UIView*)_tabBarItems [2];
    view.hidden = YES;

}

#pragma mark -Extend Method-
void setBoundsWithView(UIView *view, CGFloat r)
{
    view.layer.masksToBounds = YES; //没这句话它圆不起来
    view.layer.cornerRadius = r; //设置图片圆角的尺度。
}

void setBorderWithColor(UIView *view, CGFloat borderWidth,UIColor * borderColor)
{
    view.layer.borderWidth = borderWidth;
    view.layer.borderColor = borderColor.CGColor;
}

@end
